var ChatLoadMoreMsg = (function(){
    
    var _loadMoreTmp = [
        '<div class="chatMsgHr" t="<%=o.type || "prev" %>" seq="<%= ((o.type || "prev") == "prev") ? -1 : 9999999999 %>">', 
            '<span class="chatMsgLoadMoreText <%=o.type || "prev" %>" teid="<%=o.teid%>" seq="<%=o.seq%>" >',
                '<%=o.text || (o.type=="next" ? "Load Next" : "Load Previous") %>',
            '</span>', 
        '</div>'
    ].join('');
    
    var _chatLoadMoreMsg = (function ChatLoadMoreMsg(){        
    }).extend(ChatMsg, 'ChatLoadMoreMsg');
    
    _chatLoadMoreMsg.prototype.render = function (chatView) {
        var _this = this;  
        this.chatView = chatView;        
        this.chatGroupID = chatView.chatPanel.chatGroupEntity['id'];
              
        if(!this.isInit){            
            this.$wrapper = $(tmpl(_loadMoreTmp, this.rawMsg));            
            this._init();            
            this.isInit = true;
            var $loadMoreBtn = this.$wrapper.find('.chatMsgLoadMoreText');
            
            
            //翻页.
            $loadMoreBtn.on('click', function(e){
                if($loadMoreBtn.hasClass('loading')){
                    return false;
                }
                $loadMoreBtn.addClass('loading');
                
                var teid = $(this).attr('teid'),
                    seq = parseInt($(this).attr('seq')),
                    teObj = page.chatGroupStore.getItem(teid),
                    pageSize = 11;
                
                if($(this).hasClass('next')){   //load next.
                    var latestSeq = teObj ? teObj['latestSeq'] : 30000;
                    page.biz.getMsgList(teid, seq, pageSize, _this.chatView.deleted).done(function(msg){
                        if(!msg['topics'] || !msg['topics'].length){
                            return _this.hide();
                        }                        
                        if(msg['topics'].length == pageSize){
                            $loadMoreBtn.attr('seq', seq + pageSize);              
                        } else {
                            _this.hide();
                        }
                        _this.chatView.addTopic(msg['topics'].reverse(), _this, 'before');
                        _this.chatView.scrollToEnd();
                        $loadMoreBtn.removeClass('loading');                                    
                    });
                } else {    // load previous.
                   
                    page.biz.getMsgList(teid, seq, pageSize, _this.chatView.deleted).done(function(msg){
                        if(!msg['topics'] || !msg['topics'].length){
                            _this.hide();
                            return false;
                        }
                        var currentSeq = Math.max(seq - pageSize, 0);
                        if(currentSeq > 0){
                            $loadMoreBtn.attr('seq', currentSeq);                            
                        } else {
                            _this.hide();
                        }
                        _this.chatView.addTopic(msg['topics'], _this);                     
                        //设置已读.
                        _this.chatView.setMsgReaded(seq);                           
                        $loadMoreBtn.removeClass('loading');            
                    });
                }
            });
        }   
             
        chatView.addMsg(this);
        return this;
    };
    return _chatLoadMoreMsg;
})();   